﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.components {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import net.wooga.pets.constants.*;

    public class GameView extends Sprite {

        private var _vectorDisplay:Sprite;
        private var _screenshot:Bitmap;
        private var _startDraggingPoint:Point;
        private var _isDragging:Boolean;
        private var _stageRectangle:Rectangle;
        private var _bottomBorder:Number;
        private var _topBorder:Number;
        private var _rightBorder:Number;
        private var _leftBorder:Number;
        private var _hospitalScale:Number;
        private var _hiddenBird:HiddenBirdDisplay;
        private var _background:Sprite;

        public static const SCALE_CHANGED:String = "SCALE_CHANGED";
        public static const MAIN_MENU_HEIGHT:int = 167;
        public static const TOP_BORDER_OFFSET:int = 154;

        public function GameView(){
            this.createVectorDisplay();
        }
        private function createVectorDisplay():void{
            this._vectorDisplay = new Sprite();
            addChild(this._vectorDisplay);
        }
        public function getVectorDisplayOffset():Point{
            return (new Point(this._vectorDisplay.x, this._vectorDisplay.y));
        }
        public function addBackground(_arg1:Sprite):void{
            this._background = Sprite(this._vectorDisplay.addChild(_arg1));
            _arg1.addEventListener(Event.ADDED_TO_STAGE, this.background_addedToStage);
        }
        private function background_addedToStage(_arg1:Event):void{
            var _local2:GameBackground = GameBackground(_arg1.target);
            _local2.removeEventListener(Event.ADDED_TO_STAGE, this.background_addedToStage);
            this.calculateBorders();
            this.setPositionAndCheckBorders(new Point(x, y));
        }
        public function addTruck(_arg1:Sprite):void{
            this._vectorDisplay.addChild(_arg1);
        }
        public function addHospital(_arg1:Sprite):void{
            this._vectorDisplay.addChild(_arg1);
        }
        public function setCachedAsBitmap(_arg1:Boolean):void{
            if (_arg1){
                this.createScreenshot();
                this._vectorDisplay.visible = false;
                parent.scrollRect = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
            } else {
                this.destroyScreenshot();
                parent.scrollRect = null;
                this._vectorDisplay.visible = true;
            };
        }
        private function destroyScreenshot():void{
            if (((this._screenshot) && (!((getChildIndex(this._screenshot) == -1))))){
                removeChild(this._screenshot);
                this._screenshot.bitmapData.dispose();
                this._screenshot = null;
            };
        }
        private function createScreenshot():void{
            this._screenshot = new Bitmap();
            var _local1:Number = stage.mouseX;
            var _local2:Number = stage.mouseY;
            var _local3:Rectangle = new Rectangle((_local1 - 760), (_local2 - 600), (stage.stageWidth * 2), (stage.stageHeight * 2));
            var _local4:BitmapData = new BitmapData(_local3.width, _local3.height, true, 0);
            _local4.lock();
            this._screenshot.x = (_local3.x - x);
            this._screenshot.y = (_local3.y - y);
            var _local5:Matrix = new Matrix();
            _local5.translate((-(_local3.x) + x), (-(_local3.y) + y));
            _local4.draw(this, _local5);
            _local4.unlock();
            this._screenshot.bitmapData = _local4;
            addChild(this._screenshot);
        }
        public function scale(_arg1:Number, _arg2:Number, _arg3:Number):void{
            log("scale");
            var _local4:Number = (_arg1 / this._vectorDisplay.scaleX);
            var _local5:Matrix = this._vectorDisplay.transform.matrix;
            _local5.translate(-(_arg2), -(_arg3));
            _local5.scale(_local4, _local4);
            _local5.translate(_arg2, _arg3);
            this._vectorDisplay.transform.matrix = _local5;
            dispatchEvent(new Event(SCALE_CHANGED));
            this.calculateBorders();
            this.calculateDraggingPosition(true);
        }
        private function stopDragging(_arg1:Event=null):void{
            this._isDragging = false;
            this.setCachedAsBitmap(false);
            parent.scrollRect = null;
            stage.removeEventListener(MouseEvent.MOUSE_UP, this.stopDragging);
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.stage_mouseMove);
        }
        public function startDragging():void{
            this._isDragging = true;
            this.createStartDraggingPoint();
            this.calculateBorders();
            this.setCachedAsBitmap(true);
            this._stageRectangle = new Rectangle(0, 0, stage.stageWidth, (stage.stageHeight - MAIN_MENU_HEIGHT));
            parent.scrollRect = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
            this.calculateDraggingPosition();
            stage.addEventListener(MouseEvent.MOUSE_MOVE, this.stage_mouseMove);
            stage.addEventListener(MouseEvent.MOUSE_UP, this.stopDragging);
        }
        private function createStartDraggingPoint():void{
            this._startDraggingPoint = new Point();
            this._startDraggingPoint.x = (stage.mouseX - x);
            this._startDraggingPoint.y = (stage.mouseY - y);
        }
        public function calculateBorders():void{
            var _local1:Rectangle = this._background.getRect(stage);
            var _local2:Rectangle = getRect(stage);
            var _local3:Number = (_local2.height - _local1.height);
            this._bottomBorder = ((y - _local1.y) + _local3);
            this._topBorder = ((((y + GameConstants.STAGE_HEIGHT) - TOP_BORDER_OFFSET) - _local1.height) - _local1.y);
            this._rightBorder = -(this.getVectorDisplayOffset().x);
            this._leftBorder = ((GameConstants.STAGE_WIDTH - _local1.width) + this._rightBorder);
            this._bottomBorder = (this._bottomBorder + (GameConstants.ROOM_HEIGHT * this._hospitalScale));
        }
        private function stage_mouseMove(_arg1:MouseEvent):void{
            this.calculateDraggingPosition();
        }
        private function calculateDraggingPosition(_arg1:Boolean=false):void{
            var _local2:Point = new Point();
            var _local3:Point = new Point(stage.mouseX, stage.mouseY);
            if (((!(_arg1)) && (this._stageRectangle.containsPoint(_local3)))){
                _local2.x = (_local3.x - this._startDraggingPoint.x);
                _local2.y = (_local3.y - this._startDraggingPoint.y);
            } else {
                _local2.x = x;
                _local2.y = y;
            };
            this.setPositionAndCheckBorders(_local2);
        }
        public function setPositionAndCheckBorders(_arg1:Point):void{
            if (_arg1.y > this._bottomBorder){
                _arg1.y = this._bottomBorder;
            } else {
                if (_arg1.y < this._topBorder){
                    _arg1.y = this._topBorder;
                };
            };
            if (_arg1.x > this._rightBorder){
                _arg1.x = this._rightBorder;
            } else {
                if (_arg1.x < this._leftBorder){
                    _arg1.x = this._leftBorder;
                };
            };
            var _local2:Number = (stage.stageHeight - TOP_BORDER_OFFSET);
            if (this._vectorDisplay.height < _local2){
                _arg1.y = this._topBorder;
            };
            x = _arg1.x;
            y = _arg1.y;
        }
        public function get isDragging():Boolean{
            return (this._isDragging);
        }
        public function isPositionBottomBorder():Boolean{
            return ((y <= this._topBorder));
        }
        public function set hospitalScale(_arg1:Number):void{
            this._hospitalScale = _arg1;
        }
        public function get bottomBorder():Number{
            this.calculateBorders();
            return (this._bottomBorder);
        }
        public function get topBorder():Number{
            this.calculateBorders();
            return (this._topBorder);
        }
        public function get rightBorder():Number{
            this.calculateBorders();
            return (this._rightBorder);
        }
        public function get leftBorder():Number{
            this.calculateBorders();
            return (this._leftBorder);
        }
        public function addHiddenBird():void{
            this._hiddenBird = HiddenBirdDisplay(this._vectorDisplay.addChild(new HiddenBirdDisplay()));
            this._hiddenBird.x = (((this._background.width - this._hiddenBird.width) - 140) * Math.random());
            this._hiddenBird.y = ((GameConstants.STAGE_HEIGHT - MAIN_MENU_HEIGHT) - this._hiddenBird.height);
        }
        public function removeHiddenBird():void{
            if (this._hiddenBird){
                this._hiddenBird.remove();
                this._hiddenBird = null;
            };
        }
        public function get vectorDisplay():Sprite{
            return (this._vectorDisplay);
        }
        public function get background():Sprite{
            return (this._background);
        }

    }
}//package net.wooga.pets.views.components 
